package com.example.libo.huihui.fragment;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.libo.huihui.BR;
import com.example.libo.huihui.R;
import com.example.libo.huihui.activity.ProductDetailActivity;
import com.example.libo.huihui.adapter.BindAdapter;
import com.example.libo.huihui.application.MyApplication;
import com.example.libo.huihui.been.ShaiWuEntity;
import com.example.libo.huihui.databinding.FragmentShaiwuBinding;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ShaiwuFragment extends BaseFragment implements Callback<ShaiWuEntity>, SwipeRefreshLayout.OnRefreshListener {
    private ArrayList<ShaiWuEntity.DataBean.ListBean> datas = new ArrayList();
    private BindAdapter bindAdapter;
    private FragmentShaiwuBinding bind;
    private int page = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = DataBindingUtil.inflate(LayoutInflater.from(getContext()),R.layout.fragment_shaiwu,container,false).getRoot();
        bind = DataBindingUtil.bind(view);
        initView();
        return view;
    }

    private void initView() {
        //根据json数据中的实体类种类进行录入map的子类型，包括实体对象类类型，对应布局Id和变量variableId
        HashMap<Class <?>,Pair<Integer,Integer>> map = new HashMap<>();
        map.put(ShaiWuEntity.DataBean.ListBean.class,Pair.create(R.layout.item_shaidan, BR.shaiwu));

        bindAdapter = new BindAdapter(map,datas,getActivity());
        bind.rvShaiwu.setLayoutManager(new LinearLayoutManager(getActivity()));
        bind.rvShaiwu.setAdapter(bindAdapter);

        bindAdapter.setCallBack(new BindAdapter.ILoadMore() {
            @Override
            public void loadMore() {
                MyApplication.getHttpService().getShaiWu(page++).enqueue(ShaiwuFragment.this);
            }
        });

        bindAdapter.setOnItemClickListener(new BindAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Intent intent = new Intent(getActivity(), ProductDetailActivity.class);
                intent.putExtra("url",datas.get(position).getShaidan().getUrl());
                startActivity(intent);
            }
        });

        bind.srl.setOnRefreshListener(this);
    }

    @Override
    public void onResponse(Call<ShaiWuEntity> call, Response<ShaiWuEntity> response) {
        bind.srl.setRefreshing(false);
        datas.addAll(response.body().getData().getList());
        bindAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(Call<ShaiWuEntity> call, Throwable t) {

    }

    @Override
    public void onRefresh() {
        datas.clear();
        page = 0;
        MyApplication.getHttpService().getShaiWu(page++).enqueue(this);
    }

    @Override
    protected void lazyLoad() {
        MyApplication.getHttpService().getShaiWu(page++).enqueue(this);
    }
}
